package com.woniu34.auth.controller;

import com.woniu.common.core.controller.BaseController;
import com.woniu.common.core.domain.AjaxResult;

import com.woniu.common.token.TokenServiceUtil;
import com.woniu34.auth.service.LoginService;
import com.woniu34.auth.domain.LoginModel;
import com.woniu34.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginController extends BaseController {

    @Autowired
    private LoginService loginService;


    @Autowired
    private TokenServiceUtil tokenServiceUtil;




    @PostMapping("/login")
    public AjaxResult  doLogin(@RequestBody LoginModel loginModel){
        String uuid  =  loginModel.getUuid();
        User user = loginService.doLogin(uuid, loginModel.getUsername(), loginModel.getPassword(),loginModel.getCode());
        ///////////////生成jwt token/////

        String token = tokenServiceUtil.generateToken(user.getUserId(), user.getUserName(), user.getUserAccount());

        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("token",token);
        return ajaxResult;
    }


    @PostMapping("/weblogin")
    public AjaxResult  doWebLogin(@RequestBody LoginModel loginModel){

        User user = loginService.doWebLogin(loginModel.getUsername(), loginModel.getPassword());
        ///////////////生成jwt token/////

        String token = tokenServiceUtil.generateToken(user.getUserId(), user.getUserName(), user.getUserAccount());

        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("token",token);
        return ajaxResult;
    }

}
